透過 new 與 delete 建立了一個脆弱的 「責任協議」。程式設計師必須精確預測所有執行路徑——包括提早回傳與例外狀況——以確保資源被釋放。這種方法容易導致系統性失敗。
系統性失敗
- 記憶體洩漏陷阱: 在複雜邏輯(if-else、switch)中,若未能為每一個
new配對delete將隨著時間推移逐步降低系統效能。 - 指標無效化: 例如 懸空指標 (在釋放後存取記憶體)或 重複釋放 會引發未定義行為與安全漏洞。
- 例外安全: 若在配置與釋放之間發生例外,則
delete將完全被跳過。
的 演進
C++11/14 重新設計了 <memory> 標頭,從「裸指標」轉向自動化的擁有權模型,於編譯階段即強制安全機制。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Why is 'Exception Safety' a major concern with manual memory management?
Exceptions automatically call 'delete' for you.
An exception can bypass the 'delete' statement, causing a leak.
Exceptions only occur when memory is full.
Manual management prevents exceptions from being thrown.
✅ Correct!
If an exception is thrown before the program reaches 'delete', the stack unwinds and the heap pointer is lost without deallocation.❌ Incorrect
Standard pointers do not have destructors to clean up memory during an exception throw.QUESTION 2
What is a 'Dangling Pointer'?
A pointer that was never initialized.
A pointer still pointing to memory that has already been deleted.
A pointer that points to another pointer.
A pointer used in a circular dependency.
✅ Correct!
Accessing a dangling pointer causes 'Undefined Behavior' because the memory may have been reassigned.❌ Incorrect
Uninitialized pointers are 'wild' pointers; dangling pointers are 'stale' pointers to freed memory.QUESTION 3
Which header was significantly improved in C++11 and C++14 to automate memory?
✅ Correct!
The <memory> header contains std::unique_ptr, std::shared_ptr, and std::weak_ptr.❌ Incorrect
While smart pointers are the focus, the header name is simply <memory>.QUESTION 4
In the code example, what happens if size is 2048?
The memory is deleted before returning false.
The function crashes immediately.
Memory is allocated, but the function returns early without freeing it.
The compiler catches the leak at build time.
✅ Correct!
This is the 'Leakage Trap' where logic complexity leads to forgotten deallocations.❌ Incorrect
Compilers cannot reliably detect all runtime logic leaks in manual management.QUESTION 5
What is 'Double Freeing'?
Allocating two variables at once.
Attempting to delete the same memory address twice.
Using a smart pointer inside another smart pointer.
Freeing memory and setting the pointer to nullptr.
✅ Correct!
Double freeing often leads to heap corruption and can be exploited for security attacks.❌ Incorrect
Setting a pointer to nullptr after freeing is actually a safety best-practice.Legacy Server Audit
Resource Exhaustion Analysis
A high-frequency server uses a manual buffer (new char[1024]) for every incoming connection. The function returns 'false' early on socket timeouts without calling 'delete[]'.
Q
If 1,000 timeouts occur per minute, how much memory is leaked per hour?
Solution:
1,000 timeouts/min * 60 mins = 60,000 leaks/hour. At 1KB per leak, the system loses ~60MB of RAM per hour.
1,000 timeouts/min * 60 mins = 60,000 leaks/hour. At 1KB per leak, the system loses ~60MB of RAM per hour.
Q
What is the primary danger if this server runs for several days?
Solution:
Systemic memory exhaustion. Eventually, the OS will trigger the OOM (Out of Memory) killer or the process will crash because 'new' fails to allocate more memory.
Systemic memory exhaustion. Eventually, the OS will trigger the OOM (Out of Memory) killer or the process will crash because 'new' fails to allocate more memory.